﻿
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Net;
using Common.Model;
using Elasticsearch.Net;

namespace Common.Dao.EsDao
{
    public class CompanyNoticeEsDao
    {
        public static readonly String Index = "company_notice";
        public static readonly String Type = "company_notice";

        public static void SaveCompanyNotices(List<CompanyNotice> companyNotices)
        {

            ConnectionConfiguration settings = new ConnectionConfiguration(new Uri(Config.EsPath)).RequestTimeout(TimeSpan.FromMinutes(2));
            var lowlevelClient = new ElasticLowLevelClient(settings);
            
            var list = new List<Object>();
            companyNotices.ForEach(m =>
            {
                var create = new
                {
                    index = new
                    {
                        _index = Index,
                        _type = Type,
                        _id = m.Id
                    }
                };
                var doc = new
                {
                    site_time = m.SiteTime.ToString("yyyy-MM-dd HH:mm:ss"),
                    crawling_time = m.CrawlingTime.ToString("yyyy-MM-dd HH:mm:ss"),
                    stock_code = m.StockCode,
                    title = m.Title,
                    adjunct_url = m.AdjunctUrl,
                    content = m.Content
                };
                list.Add(create);
                list.Add(doc);
            });
            var indexResponse = lowlevelClient.Bulk<StringResponse>(PostData.MultiJson(list.ToArray()));
        }
    }
}
